/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JToolTip;

import lineage.ui.customize.L1ToolTip;
import lineage.util.Util;

/**
 * 按钮.
 * 
 * @version 2014年2月10日下午4:36:26
 * @author jrwz
 */
public class L1Button extends JButton implements ActionListener {
    private static final long serialVersionUID = -3603553934801449992L;
    /** 组件的x坐标. */
    protected int _x;
    /** 组件的y坐标. */
    protected int _y;
    /** 组件的宽度. */
    protected int _width;
    /** 组件的高度. */
    protected int _height;
    /** 预设要显示的内容. */
    protected String presetText;
    /** 标签提示信息. */
    protected String toolTipText;

    protected L1Button() {
    }

    /**
     * 初始化.
     * 如要添加其他组件,应在此方法之前.
     * 
     * @param flag
     *            - 是否立即显示
     */
    protected void init(boolean flag) {
        setText(presetText);
        setBounds(_x, _y, _width, _height);
        setToolTipText(toolTipText); // 标签提示信息
        addListener(); // 监听器
        add();
        setVisible(flag);
    }

    /**
     * 增加监听器(默认增加点击事件监听).
     */
    protected void addListener() {
        addActionListener(this); // 增加按钮点击事件监听
    }

    /**
     * 在此增加其他组件(默认什么也不加).
     */
    protected void add() {
    }

    /**
     * 取得预先要显示的字符串.
     * 
     * @return 预先要显示的字符串内容
     */
    public String getPresetText() {
        return presetText;
    }

    /**
     * 设置预先要显示的字符串.
     * 
     * @param s
     *            - 预先要显示的字符串
     */
    public void setPresetText(String s) {
        presetText = s;
    }

    // 重写以便实现自定义标签提示框
    @Override
    public JToolTip createToolTip() {
        return new L1ToolTip();
    }

    // 点击按钮时调用.
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getModifiers() == 16) { // 鼠标左键单击
            switch (e.getActionCommand()) {
            case "1":
                break;
            default:
                break;
            }
            Util.message(e.getActionCommand() + " 按钮被按下");
        }
    }

    // 依鼠标位置的不同而显示不同的文本标签提示
    // @Override
    // public boolean contains(int x, int y) {
    // if (x < 10) {
    // setToolTipText("1");
    // } else {
    // setToolTipText("2");
    // }
    // return super.contains(x, y);
    // }
}
